﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MobiMenu.Restaurant.Web;
using System.Text;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;

public partial class ViewPage : System.Web.UI.Page
{
    private OutputData outputData;
    protected void Page_Load(object sender, EventArgs e)
    {
        outputData = new OutputData()
        {
            PageSize = 4,
            PageNo = 1,
            Width = "750px",
            Height = "500px",
            Caption = "Order",
            EditLink = "edit.asp",
            DeleteLink = "delete.asp",
            DetailLink = "detail.asp",
            Source = "orders",
        };

        ProcessQueryString();
    }

    private void ProcessQueryString()
    {
        string contentName;
        if (!GetRequestContentName(out contentName) && string.IsNullOrEmpty(contentName))
        {
            return;
        }

        contentName = contentName.ToLower();
        switch (contentName)
        { 
            case "orders":
                CreateOrdersOutputData();
                break;
            case "dishes":
                CreateDishesOutputData();
                break;
            case "advertisements":
                CreateAdvertisementsOutputData();
                break;
            case "ordereddishes":
                CreateOrderedDishesOutputData();
                break;
            default:
                return;
        }

        var sb = new StringBuilder();
        sb.Append("var outputData=").Append(JsonHelper<OutputData>.WriteObject(outputData)).Append(";");
        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "OutputData", sb.ToString(), true);
    }

    private void CreateOrderedDishesOutputData()
    {
        outputData.Caption = "OrderedDishes";
        outputData.Source = "ordereddishes";
        outputData.Columns.AddRange(new ColumnData[]{
            new ColumnData(){Field="id",Title="ID",Width="100px"},
            new ColumnData(){Field="dishId",Title="菜式",Width="100px"},
            new ColumnData(){Field="orderId",Title="订单",Width="100px"},
            new ColumnData(){Field="orderedTime",Title="下单时间",Width="200px", IsDateTime=true},
            new ColumnData(){Field="status",Title="此菜状态",Width="100px", EnumType="OrderedDishStatus"}}
        );
    }

    private void CreateAdvertisementsOutputData()
    {
        outputData.Caption = "Advertisements";
        outputData.Source = "advertisements";
        outputData.Columns.AddRange(new ColumnData[]{
            new ColumnData(){Field="id",Title="ID",Width="100px"},
            new ColumnData(){Field="title",Title="标题",Width="100px"},
            new ColumnData(){Field="relatedDishId",Title="相关菜式",Width="100px"},
            new ColumnData(){Field="ShowInMenu",Title="菜单中显示",Width="100px"}}
        );
    }

    private void CreateDishesOutputData()
    {
        outputData.Caption = "Dishes";
        outputData.Source = "dishes";
        outputData.Columns.AddRange(new ColumnData[]{
            new ColumnData(){Field="id",Title="ID",Width="100px"},
            new ColumnData(){Field="name",Title="菜名",Width="100px"},
            new ColumnData(){Field="ShowInMenu",Title="菜单中显示",Width="100px"},
            new ColumnData(){Field="dishType",Title="类型",Width="100px", EnumType="DishType"},
            new ColumnData(){Field="price",Title="价格",Width="100px"},
            new ColumnData(){Field="useDiscountPrice",Title="使用优惠价",Width="100px"},
            new ColumnData(){Field="discountPrice",Title="优惠价",Width="100px"}}
        );
    }

    private void CreateOrdersOutputData()
    {
        outputData.Caption = "Orders";
        outputData.Source = "orders";
        outputData.Columns.AddRange(new ColumnData[]{
            new ColumnData(){Field="id",Title="ID",Width="100px"},
            new ColumnData(){Field="orderedTime",Title="下单时间",Width="200px", IsDateTime=true},
            new ColumnData(){Field="waiterId",Title="服务员工号",Width="100px"},
            new ColumnData(){Field="tableId",Title="桌号",Width="100px"},
            new ColumnData(){Field="status",Title="订单状态",Width="100px", EnumType="OrderStatus"}}
        );
    }

    private bool GetRequestContentName(out string contentName)
    {
        contentName = null;
        if ("orders".Equals(ClientQueryString, StringComparison.InvariantCultureIgnoreCase))
        {
            contentName = "orders";
            return true;
        }

        if ("dishes".Equals(ClientQueryString, StringComparison.InvariantCultureIgnoreCase))
        {
            contentName = "dishes";
            return true;
        }

        if ("advertisements".Equals(ClientQueryString, StringComparison.InvariantCultureIgnoreCase))
        {
            contentName = "advertisements";
            return true;
        }

        if ("ordereddishes".Equals(ClientQueryString, StringComparison.InvariantCultureIgnoreCase))
        {
            contentName = "ordereddishes";
            return true;
        }

        return false;
    }
}